package cn.doctorpda.conference.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.meeting.MyMeetingInfoItem;
import cn.doctorpda.conference.utils.ImageUtils;
import cn.doctorpda.conference.utils.ScreenUtils;

/**
 * Created by huangjinlong on 2016/3/29.
 */
public class MyMeetingAdapter extends RecyclerView.Adapter<MyMeetingAdapter.ViewHolder> {

    //    private List<String> datas = new ArrayList<>();
    private ArrayList<MyMeetingInfoItem> infoList = new ArrayList<MyMeetingInfoItem>();
    private OnItemClickListener listener;
    private Context context;


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, MyMeetingInfoItem data);
    }

    public void clear() {
        this.infoList.clear();
    }

    public void addAll(ArrayList<MyMeetingInfoItem> meetingList) {
        this.infoList.addAll(meetingList);
        notifyDataSetChanged();
    }

//    public void add(String data) {
////        this.datas.add(2, data);
//        notifyItemInserted(2);
//    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_meeting, parent, false);
        LinearLayout.LayoutParams la = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(parent.getContext()), ScreenUtils.getScreenHeight(parent.getContext()) /4);
        view.setLayoutParams(la);
        this.context = parent.getContext();
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final int pos = position;
        final MyMeetingInfoItem itemInfo = infoList.get(pos);
//        holder.tv.setText(data);
        ImageUtils.loadImage(context, itemInfo.getCLIENT_BANNER(), holder.imvMeetingPic);
        holder.tvDate.setText(itemInfo.getDATE_STR());
        holder.tvAddress.setText(itemInfo.getADDRESS());
        holder.tvTitle.setText(itemInfo.getNAME());
        holder.tvState.setText(itemInfo.getCON_STATUS());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position, infoList.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return infoList.size();
    }

    //item元素
    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitle, tvDate, tvAddress, tvState;//会议标题,会议时间，会议地址,会议状态
        ImageView imvMeetingPic;//会议图片

        public ViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.txt_item_title);
            tvDate = (TextView) itemView.findViewById(R.id.txt_item_date);
            tvAddress = (TextView) itemView.findViewById(R.id.txt_item_address);
            tvState = (TextView) itemView.findViewById(R.id.btn_item_state);
            imvMeetingPic = (ImageView) itemView.findViewById(R.id.imv_meeting);
        }
    }
}
